package Solutions

import "sort"

type Pair struct {
	V int
	L int
}

func largestValsFromLabels(values []int, labels []int, numWanted int, useLimit int) int {
	var pairs = make([]Pair, len(values))
	for i := range values {
		pairs[i].V = values[i]
		pairs[i].L = labels[i]
	}
	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i].V > pairs[j].V
	})
	count := map[int]int{}
	ans := 0
	for i := 0; i < len(pairs) && numWanted > 0; i++ {
		if count[pairs[i].L] < useLimit {
			count[pairs[i].L]++
			numWanted--
			ans += pairs[i].V
		}
	}
	return ans
}
